package com.tyc.kafka.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * 自定义分区
 */
public class MyPartitioner implements Partitioner {
    /**
     *
     * @param topic
     * @param key
     * @param keyBytes 序列化之后的key
     * @param value
     * @param valueBytes 序列化之后的value
     * @param cluster
     * @return
     */
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        String msg = value.toString();
        int partition = 0;
        if(msg.startsWith("1")){
            partition = 1;
        }else if(msg.startsWith("2")){
            partition = 2;
        }else if(msg.startsWith("3")){
            partition = 3;
        }
        return partition;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
